<?php
class Media {
	
	/**
     * @var object Zend DB Adapter
     */
	private $_db;
    /**
     * @var string
     */

	public function __construct()
    {
        $this->_db = Zend_Registry::get('db');
    }
	
	public function addBanner($post_arr, $filename)
	{
		$banner_title = $post_arr['txtBannerTitle'];
		$banner_desc = addslashes($post_arr['txtBannerDesc']);
		$banner_url = $post_arr['txtBannerURL'];
		$is_active = $post_arr['txtIsActive'];
		$banner_type = $post_arr['txtDir'];
		if($is_active == "")
		{
			$is_active = 0;
		}
		
		$sql = "INSERT INTO aml_banners(banner_type, banner_filename, banner_title, banner_desc, banner_url, is_active)
				VALUES('" . $banner_type . "', '" . $filename . "', '" . $banner_title . "', '" . $banner_desc . "', '" . $banner_url . "', " . $is_active . ")";
		$add_banner = $this->_db->exec($sql);
		if($add_banner)
		{
			header("Location: /admin/content/media/dir/" . $banner_type . "/STAT/add");
		}
	}
	
	public function updateBanner($post_arr, $filename)
	{
		$banner_id = $post_arr['txtBannerId'];
		$banner_image = $post_arr['txtBannerImg'];
		$banner_title = $post_arr['txtBannerTitle'];
		$banner_desc = addslashes($post_arr['txtBannerDesc']);
		$banner_url = $post_arr['txtBannerURL'];
		$banner_img = $post_arr['txtBannerImg'];
		if(isset($post_arr['txtIsActive']))
		{
			$is_active = 1;
		} else {
			$is_active = 0;
		}
		
		$banner_dir = $post_arr['txtDir'];
		
		if($filename == "")
		{
			$img = $banner_img;
		} else {
			$img = $filename;
		}
		
		$sql = "UPDATE aml_banners
				SET banner_filename = '" . $img . "', banner_title = '" . $banner_title . "', banner_desc = '" . $banner_desc . "', banner_url = '" . $banner_url . "', is_active = " . $is_active . "
				WHERE banner_id = " . $banner_id . " ";
		$update_banner = $this->_db->exec($sql);
		if($update_banner)
		{
			header("Location: /admin/content/media/dir/" . $banner_dir);
		}
	}
	
	public function delImage($filepath)
	{
		$img_arr = explode("/", $filepath);
		$filename = end($img_arr);
		$sql = "DELETE FROM aml_banners
				WHERE banner_filename = '" . $filename . "' ";
		$del_banner = $this->_db->exec($sql);		
	}
	
	public function getBannerDetailsByID($banner_id)
	{
		$sql = "SELECT * 
				FROM aml_banners
				WHERE banner_id = '" . $banner_id . "' ";
		$result = $this->_db->fetchRow($sql);
		
		return $result;
	}
	
	public function getBannerDetails($banner_type)
	{
		$sql = "SELECT banner_id, banner_filename, is_active 
				FROM aml_banners
				WHERE banner_type = '" . $banner_type . "' ";
		$result = $this->_db->fetchAll($sql);
		
		return $result;
	}
	
	public function getFilenameById($banner_id)
	{
		$sql = "SELECT banner_filename
				FROM aml_banners
				WHERE banner_id = " . $banner_id;
		
		$result = $this->_db->fetchRow($sql);
		
		return $result['banner_filename'];
	}

	public function deleteBannerById($banner_id, $banner_dir)
	{
		$filename = $this->getFilenameById($banner_id);
		$del_img = unlink("images/media/" . $banner_dir . "/" . $filename);
		
		if($del_img) {
			$sql = "DELETE FROM aml_banners
					WHERE banner_id = " . $banner_id;
			
			$this->_db->exec($sql);
		}
	}
	
	public function changeBannerStatus($banner_id)
	{
		$sql_getstatus = "SELECT is_active 
				FROM aml_banners
				WHERE banner_id = " . $banner_id;
		$result = $this->_db->fetchRow($sql_getstatus);
		$status = $result['is_active'];
		
		if($status == 1) {
			$change_status = 0;
		} else {
			$change_status = 1;
		}
		
		$sql_updatestatus = "UPDATE aml_banners
							 SET is_active = " . $change_status . "
							 WHERE banner_id = " . $banner_id;
		$this->_db->exec($sql_updatestatus);		
	}
}
?>